﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;


namespace ThangNM_WinSV
{

    [RunInstaller(true)]
    public partial class ProjectInstaller : System.Configuration.Install.Installer
    {
        //public ProjectInstaller()
        //{
        //    InitializeComponent();
        //}



        private string s_usage = "InstallUtil.exe /i /serviceName=xxx /displayName=xxx /description=xxx \"TQDT_TT.MO_TK.exe\"";
        public override void Install(System.Collections.IDictionary stateSaver)
        {
            if (this.Context.Parameters.ContainsKey("serviceName"))
            {
                this.serviceInstaller2.ServiceName = this.Context.Parameters["serviceName"];
            }
            else
            {
                Console.WriteLine(s_usage);
                throw new InstallException("Missing parameter 'serviceName'");
            }

            if (this.Context.Parameters.ContainsKey("displayName"))
            {
                this.serviceInstaller2.DisplayName = this.Context.Parameters["displayName"];
            }
            else
            {
                Console.WriteLine(s_usage);
                throw new InstallException("Missing parameter 'displayName'");
            }

            if (this.Context.Parameters.ContainsKey("description"))
            {
                this.serviceInstaller2.Description = this.Context.Parameters["description"];
            }
            else
            {
                Console.WriteLine(s_usage);
                throw new InstallException("Missing parameter 'description'");
            }

            base.Install(stateSaver);
        }

        public override void Uninstall(System.Collections.IDictionary savedState)
        {
            if (this.Context.Parameters.ContainsKey("serviceName"))
            {
                this.serviceInstaller2.ServiceName = this.Context.Parameters["serviceName"];
            }
            else
            {
                Console.WriteLine(s_usage);
                throw new InstallException("Missing parameter 'serviceName'");
            }

            if (this.Context.Parameters.ContainsKey("displayName"))
            {
                this.serviceInstaller2.DisplayName = this.Context.Parameters["displayName"];
            }
            else
            {
                Console.WriteLine(s_usage);
                throw new InstallException("Missing parameter 'displayName'");
            }

            if (this.Context.Parameters.ContainsKey("description"))
            {
                this.serviceInstaller2.Description = this.Context.Parameters["description"];
            }
            else
            {
                Console.WriteLine(s_usage);
                throw new InstallException("Missing parameter 'description'");
            }

            base.Uninstall(savedState);
        }


        public ProjectInstaller()
        {

            //This call is required by the Component Designer.
            InitializeComponent();

            //Add initialization code after the call to InitializeComponen
            this.serviceInstaller2.ServiceName = Properties.Settings.Default.Service_Name;
            this.serviceInstaller2.DisplayName = Properties.Settings.Default.Service_Display_Name;
            this.serviceInstaller2.Description = Properties.Settings.Default.Service_Description;
        }
    }
}
